library(tidyverse)
library(lubridate)
library(forecast)
library(ggrepel)
library(rvest)
library(plotly)
library(xtable)
dja <- read_csv("data/DJA.csv",skip = 4)
gold <- read_csv("data/GOLD_1791-2018.csv",skip = 3)
interest_rate <- read_csv("data/INTERESTRATE_1857-2018.csv",skip = 1)
sap <- read_csv("data/SAP_1871-2018.csv", skip=1)
cpi <- read_csv("data/USCPI_1774-2018.csv", skip=4)
gdp <- read_csv("data/USGDP_1790-2018.csv", skip=2)
wage <- read_csv("data/USWAGE_1774-2018.csv", skip=3)
el dja es una serie diaria, todas las demás son anuales.
dja <- dja %>%
mutate(Date = parse_date_time(Date,orders = "mdy"))
ggplotly(ggplot(dja,aes(Date, DJIA))+
geom_line()) %>%
layout(legend = list(
orientation = "h"))
summary(dja)
Date DJIA
Min. :1885-05-02 00:00:00 Min. : 24.36
1st Qu.:1916-02-09 06:00:00 1st Qu.: 70.41
Median :1946-10-14 12:00:00 Median : 199.28
Mean :1949-03-11 07:36:39 Mean : 2354.03
3rd Qu.:1982-06-06 06:00:00 3rd Qu.: 1002.07
Max. :2018-09-10 00:00:00 Max. :26616.71
dja %>%
mutate(dif = (DJIA - lag(DJIA, default = DJIA[1]))/lag(DJIA, default = DJIA[1])) %>%
ggplot(.,aes(Date, dif))+
geom_rect(fill="firebrick",
xmin=parse_date_time("01-01-1930",orders = "mdy"),
xmax=parse_date_time("01-01-1940",orders = "mdy"),
ymin=-1,
ymax=1,
alpha=0.5)+
geom_line()
armo una lista de las crisis conocidas
url <- "https://www.caproasia.com/2016/04/12/economic-crisis-since-1900-2015/"
crisis <- url %>%
read_html() %>%
html_nodes(css = 'table') %>%
html_table(header = T)
crisis <- crisis[[1]] %>%
filter(Affected %in% c("United States","Global")) %>%
separate(Period,c("desde","hasta")," – ")
Expected 2 pieces. Missing pieces filled with `NA` in 10 rows [1, 4, 5, 6, 7, 8, 9, 10, 11, 12].
#en realidad las que terminan en "s" no duran toda la década. Las agrego a mano.
# mutate(hasta = parse_date_time(case_when(grepl("s",desde)~as.numeric(str_extract(desde,"[[:digit:]]*"))+10,
# TRUE~ as.numeric(hasta)),"y"),
# desde = parse_date_time(str_extract(desde,"[[:digit:]]*"),"y"))
crisis <- crisis %>%
mutate(hasta = parse_date_time(case_when(desde=="1970s"~"1979",
desde=="1980s"~"1982",
desde == "1990s"~"1991",
TRUE~hasta),"y"),
desde = parse_date_time(case_when(desde=="1970s"~"1973",
desde=="1980s"~"1981",
desde=="1990s"~"1990",
TRUE~desde),"y"))
# tabla <- crisis[[1]] %>%
# filter(Affected %in% c("United States","Global")) %>%
# select(-Region, - Affected) %>%
# xtable(.)
#En la consola
# print(tabla, include.rownames = F)
crisis_largas <- na.omit(crisis)
crisis_puntuales <- crisis %>%
filter(is.na(hasta))
dja <- dja %>%
mutate(dif = (DJIA - lag(DJIA, default = DJIA[1]))/lag(DJIA, default = DJIA[1]))
ggplot()+
geom_rect(data= crisis_largas,
aes(xmin=crisis_largas$desde,
xmax=crisis_largas$hasta),
fill="firebrick",
ymin=-1,
ymax=1,
alpha=0.5)+
geom_line(data = dja,aes(Date, dif))+
geom_vline(data=crisis_puntuales, aes(xintercept=desde), color = "red", linetype="dashed")
NA
Este gráfico me da la sensación de que todo estuviera corrido a la derecha (mirando las crisis puntuales vs los picos)
gold %>%
ggplot(., aes(Year, `New York Market Price (U.S. dollars per fine ounce)`))+
geom_line(size=1)+
geom_vline(xintercept = 1971, color = "red")+
geom_label_repel(data=data_frame(),aes(x=1971,y=1000,label="Fin del patrón oro"),nudge_x = -5,force=10,size=7)+
theme_minimal()+
labs(y="Dólares por onza de oro", x="Año")+
theme(text = element_text(size = 20))
ggsave("plots/oro.png", dpi=300, width = 10, height = 7, scale=1)
graf <- interest_rate %>%
gather(type,rate,2:4) %>%
ggplot(., aes(Year,rate,color=type))+
geom_line()+
guides(color=guide_legend(nrow=2,byrow=TRUE))+
theme(legend.position = "bottom")
ggplotly(graf) %>%
layout(legend = list(
orientation = "h"
)
)
sap %>%
summary()
Year The S&P Index Average for January Annual Yield
Min. :1871 Min. : 3.240 Min. :1.140
1st Qu.:1908 1st Qu.: 7.433 1st Qu.:3.310
Median :1944 Median : 16.430 Median :4.500
Mean :1944 Mean : 261.888 Mean :4.356
3rd Qu.:1981 3rd Qu.: 122.058 3rd Qu.:5.390
Max. :2018 Max. :2791.730 Max. :8.710
NA's :1
The Accumulated S&P Index Average for January
Min. : 1.0
1st Qu.: 11.5
Median : 122.0
Mean : 21442.8
3rd Qu.: 5360.0
Max. :298189.7
sap %>%
gather(type, value,2:4) %>%
mutate(type= case_when(type=="The S&P Index Average for January"~"The S&P Index\nAverage for January",
type=="The Accumulated S&P Index Average for January"~"The Accumulated S&P\nIndex Average for January",
TRUE~type)) %>%
ggplot(.,aes(Year,value, color=type))+
geom_line()+
facet_grid(type~.,scale="free")+
theme(legend.position = "bottom",
strip.text.y = element_text(angle = 0))
ts(sap$`Annual Yield`, start=min(sap$Year), frequency = 1) %>%
na.omit() %>%
auto.arima(.)
Series: .
ARIMA(0,1,2)
Coefficients:
ma1 ma2
-0.0141 -0.3007
s.e. 0.0802 0.0810
sigma^2 estimated as 0.4111: log likelihood=-141.37
AIC=288.73 AICc=288.9 BIC=297.68
cpi %>%
ggplot(., aes(Year, `U.S. Consumer Price Index *`))+
geom_line(size=1)+
geom_vline(xintercept = 1971, color = "red")+
geom_label_repel(data=data_frame(),aes(x=1971,y=100,label="Fin del patrón oro"),nudge_x = -5,force=10,size=7)+
theme_minimal()+
labs(y="IPC", x="Año")+
theme(text = element_text(size = 20))
ggsave("plots/cpi_orig.png", scale = 1)
Saving 7.29 x 4.5 in image
gdp %>%
ggplot(., aes(Year, `Real GDP (millions of 2012 dollars)`))+
geom_line(size=1)+
# geom_vline(xintercept = 1971, color = "red")+
# geom_label_repel(data=data_frame(),aes(x=1971,y=100,label="Fin del patrón oro"),nudge_x = -5,force=10,size=7)+
theme_minimal()+
labs(y="Real GDP", x="Año")+
theme(text = element_text(size = 30))
ggsave("plots/PBI.png", scale = 1)
Saving 7.29 x 4.5 in image
me interesa ver el PBI normalizado por el crecimiento poblacional, y además normalizado por la cantidad de oro que puede comprar (en lugar de normalizar por el CPI):
gdp <- left_join(gold, gdp, by = "Year") %>%
mutate(gdp_in_gold = `Nominal GDP per capita (current dollars)`/`New York Market Price (U.S. dollars per fine ounce)`,
Year = parse_date_time(Year,"y"))
ggplotly(ggplot(gdp,aes(Year,gdp_in_gold))+
geom_line())
A partir del 1900 pareciera que se arman 3 ciclos muy largos
Agregando referencias históricas de las crisis conocidas
library(scales) # to access breaks/formatting functions
ggplot()+
geom_rect(data= crisis_largas,
aes(xmin=crisis_largas$desde,
xmax=crisis_largas$hasta),
fill="firebrick",
ymin=-Inf,
ymax=Inf,
alpha=0.5)+
geom_line(size=1,
data = gdp %>%
filter(Year>parse_date_time(1900,"y"))
,aes(Year, gdp_in_gold))+
geom_vline(data=crisis_puntuales, aes(xintercept=desde), color = "red", linetype="dashed")+
geom_vline(xintercept = parse_date_time(1971,"y"),color = "gold")+ #fin del patron oro
scale_x_datetime(date_breaks = "15 years",labels = date_format("%Y") )+
theme_minimal()
ggsave("plots/gdp_in_gold_eda.PNG", dpi = 300, width = 10,height = 6)
La guerra de sesesión de EEUU fué entre el 12 de abril de 1861 y el 9 de abril de 1865
A partir de ahí el pbi en oro crece hasta el fin del patron oro.
wage %>%
summary()
Year Costs of Unskilled Labor (index 1860 = 100)
Min. :1774 Min. : 31.00
1st Qu.:1835 1st Qu.: 79.65
Median :1896 Median : 143.50
Mean :1896 Mean : 2499.49
3rd Qu.:1956 3rd Qu.: 1682.75
Max. :2017 Max. :19640.40
Production Workers Hourly Compensation (nominal dollars)
Min. : 0.020
1st Qu.: 0.060
Median : 0.150
Mean : 3.906
3rd Qu.: 2.555
Max. :32.390
NA's :16
Podemos deflactar el salario horario por el CPI
ggplotly(
wage %>%
left_join(cpi,by="Year") %>%
na.omit() %>%
mutate(salario_horario_real = `Production Workers Hourly Compensation (nominal dollars)`/`U.S. Consumer Price Index *`) %>%
ggplot(.,aes(Year,salario_horario_real))+
geom_line()
)
El salario real tiene una tendencia creciente hasta el 78’ y allí se estanca hasta el 2013
wg_gold <- wage %>%
filter(Year>=1900) %>%
left_join(gold, gdp, by = "Year") %>%
mutate(wg_in_gold = `Production Workers Hourly Compensation (nominal dollars)`/`New York Market Price (U.S. dollars per fine ounce)`,
Year = parse_date_time(Year,"y")) %>%
na.omit()
ggplot()+
geom_rect(data= crisis_largas,
aes(xmin=crisis_largas$desde,
xmax=crisis_largas$hasta),
fill="firebrick",
ymin=-Inf,
ymax=Inf,
alpha=0.5)+
geom_line(size=1,data = wg_gold, aes(Year, wg_in_gold))+
geom_vline(data=crisis_puntuales, aes(xintercept=desde), color = "red", linetype="dashed")+
geom_vline(xintercept = parse_date_time(1971,"y"),color = "gold")+ #fin del patron oro
scale_x_datetime(date_breaks = "15 years",labels = date_format("%Y") )+
theme_minimal()
ggsave("plots/wg_in_gold_eda.PNG", dpi = 300, width = 10,height = 6)
Se ven los mismos tres períodos. Pero a diferencia del GDP, el período 1980-2012 tiene un nivel más bajo que el anterior.
¿ Si quisieramos comparar ingrsos con algún revenue tendríamos usar S&P o DJA?